<?php

namespace App\Http\Controllers;

use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;

class MailController extends Controller {

    public $code;
    public $msg;
    public $data;
    //来源ip
    private $referer;
    //允许请求ip
    private $allow_ip = [
        '36.7.159.86',
        '117.71.59.101',
        '118.89.233.35',
        '127.0.0.1',
        '::1'
    ];

    public function __construct($code = '401', $msg = 'verify failed', $data = []) {
        $this->code = $code;
        $this->msg = $msg;
        $this->data = $data;
        $this->referer = $_SERVER['REMOTE_ADDR'];
        $this->validation();
    }

    public function validation() {
        if (in_array($this->referer, $this->allow_ip)) {
            $this->code = '405';
            $this->msg = 'method not allowed';
        } else {
            $this->code = '401';
            $this->msg = 'verify failed';
        }
        return response()->redata($this->code, $this->msg, $this->data);
    }

    /**
     * 索引
     * method GET
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        return $this->validation();
    }

    /**
     * 创建
     * method GET
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        //
        return 'this is create';
    }

    /**
     * 保存
     * method POST
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request) {

        if ($this->code != '401') {
            $jsonstr = $request->input('jsonstr');
            $data = json_decode($jsonstr, true);
            foreach ($data['receiver'] as $k=>$rows) {
                $viewdata = [
                    'content'=>$data['emailinfo']['content'][$k],
                ];
                Mail::queue('emails.email', $viewdata, function($message) use($data, $rows) {
                    $message->from($data['emailinfo']['from'], $data['emailinfo']['sender']);
                    if (!empty($data['emailinfo']['attachment'])) {
                        $message->attach($data['emailinfo']['attachment']);
                    }
                    $message->subject($data['emailinfo']['subject']);
                    $message->to($rows['email'],'亲亲~');
                });
            }
            return response()->redata('200', 'execute the task successfully');
        } else {
            return response()->redata('103', $this->msg, $this->data);
        }
    }

    /**
     * 显示
     * method GET
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        //
        return 'this is show22';
    }

    /**
     * 编辑
     * method GET
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        //
        return 'this is edit';
    }

    /**
     * 更新
     * method PUT/PATCH
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        //
        return 'this is update';
    }

    /**
     * 删除
     * method DELETE
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        //
        return 'this is destory';
    }

}
